﻿// See https://aka.ms/new-console-template for more information
using Config1;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Data.SqlClient;

ConfigurationBuilder builder = new ConfigurationBuilder();

//环境变量
builder.AddEnvironmentVariables("C1_");
//控制台
builder.AddCommandLine(args);   //控制台会覆盖环境变量
//配置文件
builder.AddJsonFile("config.json",optional:false,reloadOnChange:true); //配置文件覆盖控制台和环境变量
//用户机密文件AddUserSecrets<Program>:只要传当前所在程序集的某个类名即可
builder.AddUserSecrets<Program>(); //用户几米文件覆盖-->配置文件-->控制台-->环境变量 

IConfigurationRoot configRoot =builder.Build();

ServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<TestController>();

//可以链式配置
serviceCollection
    .Configure<Config>(a => configRoot.Bind(a))
    .Configure<Proxy>(a => configRoot.GetSection("proxy").Bind(a));


using (ServiceProvider sp = serviceCollection.BuildServiceProvider())
{
    var c=sp.GetRequiredService<TestController>();
    c.Test();
}

Console.ReadKey();








